<?php
/**
* Django's templating engine (partial) PHP implementation
* @link http://djangoproject.com Django
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* Template instance.
* <p>Currently, it's only able to render templates, not to build them
* <p>Usage :</p>
* <code>//read the template file :
* $str=file_get_contents($file);
* // build the template instance :
* $tpl=new djangoTpl($str);
* // Renders it with some data
* echo $tpl->render($data);</code>
* @uses dataPath
* @author J.Ducastel <jeremie@ducastel.name>
*/
class djtplTemplate {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	* constructor alias
	* @access
	*/
	function djtplTemplate(&$str) {
		$this->__construct($str);
	}/**/

	/**
	* "real" constructor
	* @access public
	* @param string $str
	*/
	function __construct(&$str) {
		// including tags and filters
		//$path=dirname(__FILE__).'/'; //echo "<p>$path</p>";
		//include_once($path.'djangotpl-tags.lib.php4');
		//include_once($path.'djangotpl-filters.lib.php4');
		// is

		$this->_buildFromString($str);
	}/**/

	/**
	* returns a template from a file's content
	* @static
	* @param string $filename
	* @return djtplTemplate
	*/
	function & getFromFile($filename) {
		if (!file_exists($filename)) {
			$f=false;
			return $f;
		}
		$str=join('',file($filename));
		return new djtplTemplate($str);
	}
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* Render's template with some data, as a string
	* @param array $data
	* @access public
	* @return string
	*/
	function render($data=null) {
		// building context
		$context=&new djtplContext($data);
		// rendering
		$rendered='';
		// looping nodes
		for ($n=0; $n<count($this->nodes); $n++)
			$rendered.=$this->nodes[$n]->render($context);
		return $rendered;
	}/**/

	/**
	* @var array djangoTplNode
	*/
	var $nodes=array();
	// var $regex_
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* builds internal tree from string
	* @param string $str
	* @access private
	* @return bool
	*/
	function _buildFromString(&$str) {
		// spliting into tokens
		//$regex='/({%[^{}]+%})/';
		//$tokens=preg_split($this->regex_tokenspliter,$str,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
		$tokens=djtplParser::tokenize($str);
		//echo '<pre>'; print_r($tokens);
		while ($node=&djtplParser::tokensToNode($tokens)) {
			$this->nodes[]=&$node;
		}
		return true;
	}/**/
}/* end of djtplTemplate class */
?>